import { _question, _submit } from '../../common/api/test'
import computedBehavior from 'miniprogram-computed'
const app = getApp()
Component({
  behaviors: [computedBehavior],
  data: {
    id: null,
    show: false,
    currentIndex: 0,
    questlist: [],
    hasChange: false,
    buttonshow:true,
    hascount:0
  },
  computed: {
    question() {
      return this.data.questlist[this.data.currentIndex]
    },
    single () {
      let question = this.data.questlist[this.data.currentIndex]
      let single = question ? question.AnsType.includes('单选') : true
      return single
    },
    noPrev() {
      return this.data.currentIndex <= 0
    },
    noNext() {
      return this.data.currentIndex >= this.data.questlist.length - 1
    },
    numList() {
      return this.data.questlist.map(item => {
        let hasAnswer = item.Ans.some(ele => ele.checked)
        let ele = {
          Num: item.Num,
          ID: item.ID,
          hasAnswer
        }
        return ele
      })
    }
  },
  methods: {
    showCard() {
      this.setData({
        show: true
      })
    },
    hideCard() {
      this.setData({
        show: false
      })
    },
    toggleShow() {
      this.data.show = !this.data.show
      this.setData({
        show: this.data.show
      })
    },
    confirm(cb) {
      let memberid = wx.getStorageSync('memberid')
      let ans = this.data.question.Ans.filter(item => item.checked)
      ans = ans.map(item => item.Key)
      ans = ans.join(',')
      console.log('change and submit', ans)
      app.loading('记录中')
      _submit(
        memberid,
        this.data.id,
        this.data.question.ID,
        ans,
        !this.data.buttonshow
      ).then(res => {
        wx.hideLoading()
        // console.log(res)
        if (res.data.IsSuccess) {
          cb & cb()
        } else {
          wx.showModal({
            title: '对不起',
            content: '记录失败，请重试',
            showCancel: false
          })
        }
      }).catch(err => {
        wx.hideLoading()
        console.log(err)
        wx.showModal({
          title: '对不起',
          content: '记录失败，请重试',
          showCancel: false
        })
      })
    },
    submit() {
      let num = this.data.numList.filter(item => !item.hasAnswer).length
      wx.showModal({
        title: '温馨提示',
        content: num ? `确定交卷吗？\n您还有${num}题为空` : '确定交卷吗？',
        success: res => {
          if (res.confirm) {
            this.confirm(() => {
              wx.redirectTo({
                url: './result?id='+this.data.id
              })
            })
          }
        }
      })
    },
    // 跳转题目
    headTo(e) {
      let currentIndex = e.currentTarget.dataset.index
      this.confirm(() => {
        this.setData({
          currentIndex
        })
        this.hideCard()
      })
    },
    getQustions() {
      app.loading('加载中')
      let memberid = wx.getStorageSync('memberid')
      _question(this.data.id, memberid).then(res => {
        wx.hideLoading()
        if (res.data.IsSuccess) {
          let questlist = res.data.Data
          this.setData({
            questlist
          })
        }
      }).catch(err => {
        console.log(err)
        wx.hideLoading()
        wx.showModal({
          title: '对不起',
          content: '请求失败，请稍后再试',
          showCancel: false
        })
      })
    },
    prev() {
      if (this.data.currentIndex <= 0) {
        return
      }
      this.confirm(() => {
        this.data.currentIndex -= 1
        this.setData({
          currentIndex: this.data.currentIndex
        })
      })
    },
    next() {
      if (this.data.currentIndex >= this.data.questlist.length - 1) {
        return
      }
      this.confirm(() => {
        this.data.currentIndex += 1
        this.setData({
          currentIndex: this.data.currentIndex
        })
      })
    },
    // 记录答案
    bindChange(e) {
      // 记录当前题目答案是否变更
      // this.data.hasChange = true
      let value = e.detail.value.toString()
      let ans = this.data.question.Ans.map((item, index) => {
        item.checked = value.includes(item.Key)
        return item
      })
      let obj = `questlist[${this.data.currentIndex}].Ans`
      this.setData({
        [obj]: this.data.questlist[this.data.currentIndex].Ans
      })
      let num = this.data.numList.filter(item => !item.hasAnswer).length
      if(num==0)
      {
        this.setData({ buttonshow:false});
      }
    },
    onLoad(options) {
      this.data.id = options.id
    },
    onReady() { },
    onShow() {
      if (app.store.userinfo) {
        this.data.userinfo = app.store.userinfo
        this.setData({
          userinfo: this.data.userinfo
        })
        this.getQustions()
      } else {
        app.loading('加载中')
        app.launch().then(res => {
          wx.hideLoading()
          console.log(res)
          this.data.userinfo = res
          this.setData({
            userinfo: this.data.userinfo
          })
          this.getQustions()
        }).catch(err => {
          wx.hideLoading()
          console.log(err)
        })
      }
    },
    onHide() { },
    onUnload() { },
    onPullDownRefresh() { },
    onReachBottom() { },
    onShareAppMessage() { }
  }
})